package com.cd.queue.api;

import com.cd.queue.annotation.QueueOperationLog;
import com.cd.queue.cache.QueueCache;
import com.cd.queue.dto.QueueUserDto;
import com.cd.queue.jpa.domain.QueueOperationRecord;
import com.cd.queue.jpa.repo.QueueOperationRecordRepository;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
//默认单例 多例可以使用@Scope
public class QueueService {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(QueueService.class);

    @Autowired
    private QueueOperationRecordRepository recordRepo;
    //获取队列信息
    public LinkedList<QueueUserDto> getQueueInfo(String organiser) {
        return QueueCache.getQueue(organiser);
    }

    //add
    @QueueOperationLog(action = "addNormal", description = "添加到普通队列")
    public LinkedList<QueueUserDto> addNormal(String organiser,String addName) {
        //String username = AuthUtils.currentUsername();

        return QueueCache.addQueue(organiser, addName);
    }

    //remove 取消
    @QueueOperationLog(action = "revokeNormal", description = "取消排队")
    public LinkedList<QueueUserDto> revokeNormal(String organiser,String targetName) {
        //todo
        return null;
    }

    //poll 叫号
    @QueueOperationLog(action = "poll", description = "叫号")
    public LinkedList<QueueUserDto> poll(String organiser,String mode) {

        return QueueCache.callQueue(organiser,mode);
    }

    @QueueOperationLog(action = "clear", description = "清空队列")
    public LinkedList<QueueUserDto> clear(String organiser) {
        //todo 存库

        return QueueCache.clearQueue(organiser);

    }

    @QueueOperationLog(action = "batchAdd", description = "批量添加到普通队列")
    public LinkedList<QueueUserDto> batchAdd(String organiser, List<String> targetNames) {

        return QueueCache.BatchAddQueue(organiser,targetNames);
    }

    //save record
    public void saveRecord(QueueOperationRecord queueOperationRecord) {
        recordRepo.save(queueOperationRecord);
    }
}
